package com.mud.dnd.frontend.rs;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.mud.dnd.domain.common.DAOException;
import com.mud.dnd.domain.common.QueryParameter;
import com.mud.dnd.domain.common.QueryParameter.QueryParamOperator;
import com.mud.dnd.domain.model.Card;
import com.mud.dnd.domain.model.Deck;
import com.mud.dnd.ejb.DeckManager;
import com.mud.dnd.ejb.common.AuthenticationException;
import com.mud.dnd.frontend.rs.common.RestInputConverter;
 
@Stateless
@Path("/Deck")
public class DeckJSONService {
	
	
	@EJB
	DeckManager ejbService;
	
	
	
	
	@GET
	@Path("/query")
	@Produces(MediaType.APPLICATION_JSON)
	public List<Deck> getDecks(@QueryParam("id") Integer id,@QueryParam("name") String name,@QueryParam("desc") String description,@QueryParam("cardID") Integer cardID, @QueryParam("picsURL")String pictureURL,
			 @QueryParam("userName")String userName,@QueryParam("rate") Double rate,@QueryParam("rateOp") String rateOp) {
		
		
		List<QueryParameter> params = new ArrayList<QueryParameter>();
		try{
		
		
		if(id!=null)
		{
			params.add(new QueryParameter("id", QueryParamOperator.EQUALS, id));
		}
		if(name!=null)
		{
			params.add(new QueryParameter("name", QueryParamOperator.LIKE, name));
		}
		if(description!=null)
		{
			params.add(new QueryParameter("description", QueryParamOperator.LIKE, description));
		}
		if(pictureURL!=null)
		{
			params.add(new QueryParameter("pictureURL", QueryParamOperator.LIKE, pictureURL));
		}
	
		if(cardID!=null)
		{
			params.add(new QueryParameter("cardID", QueryParamOperator.EQUALS, cardID));
		}
		if(userName!=null)
		{
			params.add(new QueryParameter("author", QueryParamOperator.LIKE, userName));
		}
		if(rate!=null)
		{
			
			
			params.add(new QueryParameter("rate", RestInputConverter.queryParamOperatorConverter(rateOp), rate));
		}
		}catch(DAOException e)
		{
			return null;
		}
		


		return ejbService.getDecks(params); 

	}
	
	@GET
	@Path("/create")
	@Produces(MediaType.APPLICATION_JSON)
	public String createDeck(@QueryParam("name") String name,@QueryParam("desc") String description, @QueryParam("picsURL")String pictureURL,@QueryParam("userName")String userName,@QueryParam("sessionID")String sessionID) 
	{
		
		
		int resultID;
		try {
			resultID = ejbService.createDeck(name, description, pictureURL, userName, sessionID);
		} catch (AuthenticationException e) {
			return "ERROR: "+e.getMessage();
		} catch (DAOException e) {
			return "ERROR: "+e.getMessage();
		}
 
		return "OK The new deck ID:"+String.valueOf(resultID);  
 
	}
	
	@GET
	@Path("/update")
	@Produces(MediaType.APPLICATION_JSON)
	public String updateDeck(@QueryParam("id") int id,@QueryParam("name") String name,@QueryParam("desc") String description, @QueryParam("picsURL")String pictureURL,@QueryParam("rate") int rate,@QueryParam("userName")String userName,@QueryParam("sessionID")String sessionID) 
	{
		
		try {
			ejbService.updateDeck(id, name, description, pictureURL, rate, userName, sessionID);
		} catch (DAOException e) {
			return "ERROR: "+e.getMessage();
		} catch (AuthenticationException e) {
			return "ERROR: "+e.getMessage();
		}
 
		return "OK"; 
 
	}
	
	@GET
	@Path("/delete")
	@Produces(MediaType.APPLICATION_JSON)
	public String deleteDeck(@QueryParam("id") int id,@QueryParam("userName")String userName,@QueryParam("sessionID")String sessionID) {
		
		try {
			ejbService.deleteDeck(id, userName, sessionID);
		} catch (DAOException e) {
			return "ERROR: "+e.getMessage();
		} catch (AuthenticationException e) {
			return "ERROR: "+e.getMessage();
		}
 
		return "OK"; 
 
	}
	
	
 
	
 
}
